package com.sumainfor.pc.gmio.service.impl.tenant;

import com.sumainfor.common.Constants;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.UtilsMapper;
import com.sumainfor.pc.gmio.dao.tenant.TenantKeepMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述:
 * 租客-管家评价
 * @Auther: zhlu
 * @Date: 2019/4/6 20:32
 * @Description:
 */
@Service
public class TenantKeepService {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(TenantKeepService.class);

    @Autowired
    TenantKeepMapper TenantKeepMapper;

    @Autowired
    UtilsMapper utilsMapper;

    String CalId;

    String TenkepId;

    /**
     * 功能描述: 
     * 获取管家评价的分类集合
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/6 20:33
     */
    public List<Map<String,Object>> getEvaluateList(){
        List<Map<String,Object>> getEvaluateList=TenantKeepMapper.getEvaluateList();
        return getEvaluateList;
    }

    /**
     * 功能描述: 
     * 租客联系管家记录
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/6 20:34
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean AddTenantCallKeep(Map<String,Object>params){
        boolean result=false;
        lock.lock();
        try {
            //所有新增处理
            Map<String, Object> commonInsertParms = new HashMap<String, Object>();
            commonInsertParms.put("term", "1");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            commonInsertParms.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(commonInsertParms);

            try {
                //评价记录
                Map<String, Object> calId = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "CalId");
                    put("database", Constants.DATA_SOURCE_GMIO);
                    put("surface", "tcgmio_keep_tenant_call");
                }});
                if (calId != null) {
                    int number = Integer.valueOf(calId.get("uuid").toString());
                    CalId = ComUtils.getLocalTrmSeqNum("pjjl", number);
                } else {
                    CalId = ComUtils.getLocalTrmSeqNum("pjjl", 0);
                }
                params.putAll(commonInsertParms);
                params.put("CalId",CalId);
                params.put("ContactTime", ConvertDateTime.getCurrentTime());
                //添加租客联系管家记录
                TenantKeepMapper.AddTenantCallKeep(params);
                result=true;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
     * 功能描述: 
     * 租客对管家评价
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/6 20:46
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean AddTenantEvaKeep(Map<String,Object>params){
        boolean result=false;
        lock.lock();
        try {
            //所有新增处理
            Map<String, Object> commonInsertParms = new HashMap<String, Object>();
            commonInsertParms.put("term", "1");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            commonInsertParms.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(commonInsertParms);

            try {
                //评价记录
                Map<String, Object> tenkepId = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "TenkepId");
                    put("database", Constants.DATA_SOURCE_GMIO);
                    put("surface", "tcgmio_keep_tenant");
                }});
                if (tenkepId != null) {
                    int number = Integer.valueOf(tenkepId.get("uuid").toString());
                    TenkepId = ComUtils.getLocalTrmSeqNum("pj", number);
                } else {
                    TenkepId = ComUtils.getLocalTrmSeqNum("pj", 0);
                }
                params.putAll(commonInsertParms);
                params.put("TenkepId",TenkepId);
                params.put("EvaTime", ConvertDateTime.getCurrentTime());
                //添加租客评价管家记录
                TenantKeepMapper.AddTenantEvaKeep(params);
                result=true;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } finally {
            lock.unlock();
        }
        return result;
    }
}
